package com.yc.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.yc.entity.Collect;
import com.yc.entity.User;
import com.yc.mapper.CollectMapper;
import com.yc.service.ICollectService;
import com.yc.service.IUserService;
import com.yc.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * InnoDB free: 4096 kB 前端控制器
 * </p>
 *
 * @author ly
 * @since 2022-06-07
 */
@Controller
@RequestMapping("//collect")
public class CollectController {
    @Resource
    private CollectMapper collectMapper;
    @Resource
    private ICollectService collectService;
    @Resource
    private IUserService userService;

    @RequestMapping("addCollect")
    @ResponseBody
    public Integer addCollect(Integer uid,Integer ctid){
        Integer flag=-1;
        try {
            flag=collectMapper.addCollect(uid,ctid);
        }catch ( Exception e ){
            flag=0;
        }
        return flag;
    }

    @RequestMapping("cancelcollect")
    @ResponseBody
    public Integer cancelcollect(Integer uid,Integer ctid){
        return collectService.cancelcollect(uid,ctid);
    }

    @RequestMapping("checkCollected")
    @ResponseBody
    public List< Collect > checkCollected(Integer uid,Integer ctid){
        return collectMapper.checkCollected(uid, ctid);
    }

    @PostMapping("/querycollect")
    @ResponseBody
    public List<Collect> querycollect(String ucount){
        Result result = new Result();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUcount,ucount);
        User user = userService.getOne(queryWrapper);
        Integer uid = user.getUid();
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("uid",uid);
        return collectMapper.selectList(queryWrapper1);
    }

    /**
     * 取消一个收藏
     * @param ucount
     * @param ctid
     * @return
     */
    @PostMapping("deletecollect")
    @ResponseBody
    public Result deletecollect(String ucount, Integer ctid){
        System.out.println("========================");
        Result result = new Result();
        System.out.println("======"+ctid + ucount);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUcount,ucount);
        User user = userService.getOne(queryWrapper);
        Integer uid = user.getUid();
        if (collectMapper.deletecollect(uid,ctid)){
            result.setCode(1);
            result.setMsg("取消收藏成功");
        }else{
            result.setCode(0);
            result.setMsg("取消收藏失败");
        }
        return result;
    }

    /**
     * 取消多个收藏
     * @param ucount
     * @param str
     * @return
     */
    @PostMapping("deletecollectList")
    @ResponseBody
    public Result deletecollectList(String ucount, String str){
        Gson gson = new Gson();
        System.out.println(str);
        Integer[] ctids = gson.fromJson(str,Integer[].class);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUcount,ucount);
        User user = userService.getOne(queryWrapper);
        Integer uid = user.getUid();
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.in("ctid",ctids);
        queryWrapper1.eq("uid",uid);
        collectMapper.delete(queryWrapper1);
        Result result = new Result();
        result.setCode(1);
        result.setMsg("取消收藏成功");
        return result;
    }

}

